package com.maimao.auth.session;

import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;

import java.io.IOException;

/**
 * 当同一用户的session达到指定数量 时,会执行该 类
 *
 * @Auther: 梦学谷 www.mengxuegu.com
 */

public class CustomSessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {


    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException {
        UserDetails userDetails =
                (UserDetails) event.getSessionInformation().getPrincipal();

        throw new AuthenticationServiceException(
                String.format("[%s] 用户在另外一台电脑登录,您已被下线", userDetails.getUsername()));
    }
}
